

import java.util.Calendar;

import junit.framework.TestCase;

public class TestAppointmentCalendar extends TestCase{

	private AppointmentCalendar appointmentCalendar;
	private Appointment appointment;
	private Calendar now;
	private Calendar later;

	
	protected void setUp() throws Exception {
		appointmentCalendar = new AppointmentCalendar();
		now = Calendar.getInstance();
		later = Calendar.getInstance();
		appointment = new Appointment(now, later,"foo");
	}

	public void testAddOneAppointmentToAppointmentCalendar() throws Exception{
		AppointmentCalendar appointmentCalendar = new AppointmentCalendar();
		Appointment appointment = new Appointment(Calendar.getInstance(), Calendar.getInstance(),"foo");
		appointmentCalendar.scheduleAnAppointment(appointment);
		assertTrue(appointmentCalendar.containsAppointment(appointment));
	}
	
	public void testTwoPersonalEventsCanNotBeConcurrentlyScheduled() throws Exception{
		AppointmentCalendar appointmentCalendar = new AppointmentCalendar();
		Calendar later = Calendar.getInstance();
		later.add(Calendar.HOUR_OF_DAY, 1);
		Appointment theAppointment = new Appointment(Calendar.getInstance(), later,"foo");
		appointmentCalendar.scheduleAnAppointment(theAppointment);
		try{
			appointmentCalendar.scheduleAnAppointment(theAppointment);
			fail();
		}catch(ConcurrentAppException badEvent){
			//test passes
		}
	}
	
	public void testABusinessEventCanOverwriteAPersonalEvent() throws Exception {
		AppointmentCalendar appointmentCalendarr = new AppointmentCalendar();
		Calendar later = Calendar.getInstance();
		later.add(Calendar.HOUR_OF_DAY, 1);
		Appointment theAppointment = new Appointment(Calendar.getInstance(), later,"first");
		Appointment businessAppointment = new Appointment(Calendar.getInstance(), later, "second", AppointmentType.BUSINESS);
		appointmentCalendarr.scheduleAnAppointment(theAppointment);
		try{
			appointmentCalendarr.scheduleAnAppointment(businessAppointment);
		}catch(ConcurrentAppException badEvent){
			fail();
		}
	}

	public void testDeletedAppointmentIsReallyDeleted () throws Exception{
		appointmentCalendar.scheduleAnAppointment(appointment);
		assertTrue(appointmentCalendar.containsAppointment(appointment));
		appointmentCalendar.deleteAnAppointment(appointment);
		assertFalse(appointmentCalendar.containsAppointment(appointment));
	}
	
	public void testThisAppointmentCalendarsArrayListsAreNotStatic() throws Exception{
		AppointmentCalendar appointmentCalendar1 = new AppointmentCalendar();
		Appointment appointment = new Appointment(Calendar.getInstance(), Calendar.getInstance(),"foo");
		appointmentCalendar1.scheduleAnAppointment(appointment);
		AppointmentCalendar appointmentCalendar2 = new AppointmentCalendar();
		assertFalse(appointmentCalendar2.containsAppointment(appointment));
	}
	
	public void testHighestPriorityAppointmentOverwritesLowerPriorityAppointment() throws Exception{
		AppointmentCalendar appointmentCalendarr = new AppointmentCalendar();
		Calendar later = Calendar.getInstance();
		later.add(Calendar.HOUR_OF_DAY, 1);
		Appointment firstAppointment = new Appointment(Calendar.getInstance(), later, "first", AppointmentType.PERSONAL);
		Appointment secondAppointment = new Appointment(Calendar.getInstance(), later, "second", AppointmentType.MEDICAL);
		appointmentCalendarr.scheduleAnAppointment(firstAppointment);
		try{
			appointmentCalendarr.scheduleAnAppointment(secondAppointment);
		}catch(ConcurrentAppException badEvent){
			fail();
		}
		
	}
	
	public void testTwoEventsAtSameTimeOverlap() throws Exception{
		appointmentCalendar.scheduleAnAppointment(appointment);
		Appointment newAppointment = new Appointment(now, later, "baz", AppointmentType.PERSONAL);
		try{
			appointmentCalendar.scheduleAnAppointment(newAppointment);
			fail();
		}catch (ConcurrentAppException e) {
			
		}
	}
	public void testAppointmentCalendarReturnsCurrentAppointments() throws Exception{
		Calendar muchLater = Calendar.getInstance();
		muchLater.add(Calendar.HOUR_OF_DAY, 6);
	//	later.add(Calendar.HOUR_OF_DAY, 1);

		Appointment appointment2 = new Appointment(later,muchLater,"fooTwo", AppointmentType.MEDICAL);
		appointmentCalendar.scheduleAnAppointment(appointment2);
		assertEquals(appointment2, appointmentCalendar.currentAppointments());
	}

}
